/*
 * main_window.c
 * This file is part of gURL
 *
 * Copyright (C) 2013, 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "main_window.h"

#include "defines.h"
#include <glib/gi18n.h>
#include "gurl_conf.h"
#include "request_generator_form.h"
#include "request_viewer_form.h"
#include "requests_list.h"

enum MainWindowPage {
   mwPAGE_REQUEST_GENERATOR,
   mwPAGE_SESSION_DETAILS
};

struct _MainWindow {
    GtkWindow parent;

    GtkWidget* notebook;
    GtkWidget* requests_list;
    GtkWidget* request_viewer;
    GtkWidget* request_generator;
    GtkWidget* panel;
};

struct _MainWindowClass {
    GtkWindowClass parent_class;
};

static void on_new_session(RequestGeneratorForm* rg, Session* session,
                           MainWindow* main_window);
static void on_session_selected(RequestsList* rl, Session* session,
                                MainWindow* main_window);
static gboolean on_close_window(GtkWidget *widget, GdkEvent *event,
                                gpointer user_data);

G_DEFINE_TYPE(MainWindow, main_window, GTK_TYPE_WINDOW)

static void main_window_class_init(MainWindowClass* UNUSED(klass)) {}

static const char* const CONFIG_GROUP = "window";
static const char* const CONFIG_KEY_WIDTH = "width";
static const char* const CONFIG_KEY_HEIGHT = "height";
static const char* const CONFIG_KEY_MAXIMIZED = "maximized";
static const char* const CONFIG_KEY_LEFT_PANEL_WIDTH = "left_panel_width";

static const int DEFAULT_WND_WIDTH = 900;
static const int DEFAULT_WND_HEIGHT = 640;
static const int DEFAULT_LEFT_PANEL_WIDTH = 50;

static void main_window_init(MainWindow* self) {
    gtk_window_resize(GTK_WINDOW(self),
        config_get_int(CONFIG_GROUP, CONFIG_KEY_WIDTH, DEFAULT_WND_WIDTH),
        config_get_int(CONFIG_GROUP, CONFIG_KEY_HEIGHT, DEFAULT_WND_HEIGHT));

    if(config_get_bool(CONFIG_GROUP, CONFIG_KEY_MAXIMIZED, FALSE))
        gtk_window_maximize(GTK_WINDOW(self));
    else
        gtk_window_unmaximize(GTK_WINDOW(self));

    gtk_window_set_position(GTK_WINDOW(self), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(self), "gURL - GTK-based GUI for cURL");
    gtk_container_set_border_width(GTK_CONTAINER(self), 10);
    g_signal_connect(self, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    self->panel = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_container_add(GTK_CONTAINER(self), self->panel);
    gtk_widget_show(GTK_WIDGET(self->panel));

    self->requests_list = requests_list_new();
    gtk_widget_show(GTK_WIDGET(self->requests_list));
    gtk_paned_pack1(GTK_PANED(self->panel), self->requests_list, TRUE, FALSE);

    self->notebook = gtk_notebook_new();
    gtk_widget_show(GTK_WIDGET(self->notebook));

    self->request_generator = request_generator_form_new();
    gtk_widget_show(self->request_generator);
    request_generator_form_load_config(
        REQUEST_GENERATOR_FORM(self->request_generator));
    gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
                             self->request_generator,
                             gtk_label_new(_("Request generator")));

    self->request_viewer = request_viewer_form_new();
    gtk_widget_show(GTK_WIDGET(self->request_viewer));
    gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook), self->request_viewer,
                             gtk_label_new(_("Session details")));

    gtk_paned_pack2(GTK_PANED(self->panel), self->notebook, TRUE, FALSE);
    gtk_paned_set_position(
        GTK_PANED(self->panel),
        config_get_int(CONFIG_GROUP, CONFIG_KEY_LEFT_PANEL_WIDTH,
                       DEFAULT_LEFT_PANEL_WIDTH));

    g_signal_connect(self->request_generator, "new-session",
                     G_CALLBACK(on_new_session), self);
    g_signal_connect(self->requests_list, "select-session",
                     G_CALLBACK(on_session_selected), self);
    g_signal_connect(self, "delete-event", G_CALLBACK(on_close_window), NULL);

    gtk_widget_show(GTK_WIDGET(self));
}

static void on_new_session(RequestGeneratorForm* UNUSED(rg), Session* session,
                           MainWindow* main_window) {
    requests_list_append(REQUESTS_LIST(main_window->requests_list), session);
}

static void on_session_selected(RequestsList* UNUSED(rl), Session* session,
                                MainWindow* main_window) {
    request_viewer_set_session(
        REQUEST_VIEWER_FORM(main_window->request_viewer), session);
    if(session)
       gtk_notebook_set_current_page(GTK_NOTEBOOK(main_window->notebook),
                                     mwPAGE_SESSION_DETAILS);
}

static void main_window_save_config(MainWindow* self) {
    GtkAllocation allocation;
    gboolean maximized = gdk_window_get_state(
        gtk_widget_get_window(GTK_WIDGET(self))) & GDK_WINDOW_STATE_MAXIMIZED;
    gtk_widget_get_allocation(GTK_WIDGET(self), &allocation);
    config_set_int(CONFIG_GROUP, CONFIG_KEY_WIDTH, allocation.width);
    config_set_int(CONFIG_GROUP, CONFIG_KEY_HEIGHT, allocation.height);
    config_set_bool(CONFIG_GROUP, CONFIG_KEY_MAXIMIZED, maximized);
    config_set_int(CONFIG_GROUP, CONFIG_KEY_LEFT_PANEL_WIDTH,
                   gtk_paned_get_position(GTK_PANED(self->panel)));
    request_generator_form_save_config(
        REQUEST_GENERATOR_FORM(self->request_generator));
}

static gboolean on_close_window(GtkWidget *widget, GdkEvent *UNUSED(event),
                                gpointer UNUSED(user_data)) {
    main_window_save_config(MAIN_WINDOW(widget));
    return FALSE;
}

GtkWidget* main_window_new() {
    return g_object_new(MAIN_WINDOW_TYPE, NULL);
}
